#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    string alienOrder(vector<string>& words) {
        int len = 0;
        unordered_map<char, unordered_set<char>> m;
        unordered_map<char, unordered_set<char>> in;
        for (auto e : words)
            for (auto c : e)
                in[c];

        for (int i = 0; i < words.size() - 1; i++)
        {
            for (int j = i + 1; j < words.size(); j++)
            {
                string& s1 = words[i];
                string& s2 = words[j];
                int a = 0;
                for (; a < s1.size() && a < s2.size(); a++)
                {
                    char cur = s1[a];
                    char next = s2[a];
                    if (cur != next)
                    {
                        in[next].insert(cur);
                        m[cur].insert(next);
                        break;
                    }
                }
                if (a == s2.size() && s1.size() > s2.size()) return "";
            }
        }

        queue<char> q;
        for (char i = 'a'; i <= 'z'; i++)
            if (in.count(i) && in[i].size() == 0) q.push(i);
        string ret;

        while (q.size())
        {
            char cur = q.front(); q.pop();
            ret += cur;
            for (auto e : m[cur])
            {
                in[e].erase(cur);
                if (in[e].size() == 0)q.push(e);
            }
        }

        return ret.size() == in.size() ? ret : "";
    }
};